#!/bin/bash
# script name: nginx编译安装脚本
# author: subushi
# update date: 2023.08.15

# 补包
yum  install  -y  wget  unzip  gcc  zlib  zlib-devel  pcre  pcre-devel  openssl  openssl-devel  brotli-devel


# 下载源码包
wget https://nginx.org/download/nginx-1.23.4.tar.gz

wget --user= --ask http://mydata.subushi.top/Linux/Nginx/ngx-fancyindex-master.zip

wget --user= --ask http://mydata.subushi.top/Linux/Nginx/ngx_brotli-1.0.0rc.tar.gz


# 创建nginx组/用户
groupadd  -g  88  nginx

useradd  -g  nginx  -M  -s  /sbin/nologin  -u  88  nginx


# 解压缩
tar xf nginx-1.23.4.tar.gz

unzip ngx-fancyindex-master.zip

tar -xf ngx_brotli-1.0.0rc.tar.gz


# 编译
cd nginx-1.23.4

./configure  --user=nginx  --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module  --with-http_sub_module  --with-http_ssl_module  --with-pcre --add-module=../ngx-fancyindex-master --add-module=../ngx_brotli-1.0.0rc

make && make install


# 提供配置文件
cat <<EOF | tee /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes  auto;                   # CPU核心数
worker_rlimit_nofile 65535;               # 进程最大打开文件数
error_log  logs/error.log;
pid    logs/nginx.pid;

events {
    worker_connections  65535;            # 单个进程允许客户端最大连接数
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
                      '\$status \$body_bytes_sent "\$http_referer" '
                      '"\$http_user_agent" "\$http_x_forwarded_for"';

    sendfile      on;                     # 文件高效传输模式
    tcp_nopush    on;                     # 需要在sendfile开启模式才有效，防止网路阻塞，积极的减少网络报文段的数量
    tcp_nodelay   on;                     # 提高数据的实时响应性
    keepalive_timeout  60;                # 会话保持时间
    gzip  on;                             # 开启压缩
    charset utf-8;                        # 设置字符集
    server_tokens off;                    # 隐藏版本号

    server {
      listen  80;
      server_name  localhost;

      location / {
        root   /usr/local/nginx/html/;
        try_files \$uri \$uri/ /index.html;
        index  index.html index.htm;
      }

      error_page 404 /404.html;
        location = /40x.html {
      }

      error_page 500 502 503 504 /50x.html;
        location = /50x.html {
      }
    }
}
EOF


# 启动nginx
/usr/local/nginx/sbin/nginx